package com.ruihu.admin.module.business.tasknode.domain;

import com.ruihu.admin.module.business.task.domain.entity.TaskNodeInstEntity;

import java.util.List;

public class TaskNodeInst {

    /**
     * 当前节点实例ID
     */
    private Long currentTaskNodeInstId;

    /**
     * 下一个节点对象
     */
    private TaskNodeInstEntity nextTaskNodeInstEntity = null;

    /**
     * 上一个节点对象
     */
    private TaskNodeInstEntity preTaskNodeInstEntity = null;

    /**
     * 当前节点实例是否有效
     */
    private boolean currentTaskNodeInstIdValid = false;

    /**
     * 是否倒数第二个节点
     */
    private boolean isSecondLastNode = false;

    /**
     * 是否倒数第三个节点
     */
    private boolean isThirdLastNode = false;

    private boolean isFouthLastNode = false;

    public TaskNodeInst(List<TaskNodeInstEntity> allTaskNodeEntityList, Long currentTaskNodeInstId) {
        this.currentTaskNodeInstId = currentTaskNodeInstId;
        for (int i = 0; i < allTaskNodeEntityList.size(); i++) {
            if (allTaskNodeEntityList.get(i).getId().equals(currentTaskNodeInstId)) {
                this.currentTaskNodeInstIdValid = true;
                if (i == allTaskNodeEntityList.size() - 2) {
                    this.isSecondLastNode = true;
                } else if (i == allTaskNodeEntityList.size() - 3) {
                    this.isThirdLastNode = true;
                } else if (i == allTaskNodeEntityList.size() - 4) {
                    this.isFouthLastNode = true;
                }

                if (i > 0 && i < allTaskNodeEntityList.size() - 1) {
                    this.preTaskNodeInstEntity = allTaskNodeEntityList.get(i - 1);
                    this.nextTaskNodeInstEntity = allTaskNodeEntityList.get(i + 1);
                    break;
                } else if (i == 0 && i < allTaskNodeEntityList.size() - 1) {
                    this.nextTaskNodeInstEntity = allTaskNodeEntityList.get(i + 1);
                    break;
                } else if (i > 0 && i == allTaskNodeEntityList.size() - 1) {
                    this.preTaskNodeInstEntity = allTaskNodeEntityList.get(i - 1);
                    break;
                }
            }
        }
    }

    /**
     * 是否有效的节点
     * @return
     */
    public boolean isValidNode() {
        return this.currentTaskNodeInstIdValid;
    }

    /**
     * 是否最后一个节点
     * @return
     */
    public boolean isLastNode() {
        return this.nextTaskNodeInstEntity == null;
    }

    /**
     * 是否倒数第二个节点
     * @return
     */
    public boolean isSecondLastNode() {
        return this.isSecondLastNode;
    }

    /**
     * 是否倒数第二个节点
     * @return
     */
    public boolean isThirdLastNode() {
        return this.isThirdLastNode;
    }

    /**
     * 是否倒数第四个节点
     * @return
     */
    public boolean isFouthLastNode() {
        return this.isFouthLastNode;
    }

    /**
     * 返回下一个节点
     * @return
     */
    public TaskNodeInstEntity getNextTaskNodeInstEntity() {
        return this.nextTaskNodeInstEntity;
    }

    /**
     * 返回下一个节点ID
     * @return
     */
    public Long getNextTaskNodeInstEntityId() {
        return this.nextTaskNodeInstEntity.getId();
    }

    /**
     * 返回上一个节点
     * @return
     */
    public TaskNodeInstEntity getPreTaskNodeInstEntity() {
        return this.preTaskNodeInstEntity;
    }

    /**
     * 返回上一个节点ID
     * @return
     */
    public Long getPreTaskNodeInstEntityId() {
        return this.preTaskNodeInstEntity.getId();
    }
}
